package com.tuzhi.filestore.mq.controller;

import com.tuzhi.base.web.helper.AjaxResultHelper;
import com.tuzhi.base.web.vo.AjaxResultVO;
import com.tuzhi.common.domain.ResultBO;
import com.tuzhi.common.helper.ResultHelper;
import com.tuzhi.filestore.mq.bean.MqMsg;
import com.tuzhi.filestore.mq.config.Constant;
import com.tuzhi.filestore.mq.producter.MsgProducter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;

/**
 * 功能说明：
 *
 * @author abu
 * @version 1.1.0
 * @date 2023/2/20
 */
@RestController
@CrossOrigin
@RequestMapping("/mq")
@Slf4j
public class RabbitMQController {


    @Autowired
    MsgProducter msgProducter;


    @RequestMapping(value = "/send")
    @ResponseBody
    public AjaxResultVO send(@RequestBody MqMsg mqMsg) {
        log.warn("send msg:{}", mqMsg.toString());

        try {
            byte[] msgBody = mqMsg.toString().getBytes("gbk");
            Message message = new Message(msgBody);
            msgProducter.sendMsg(Constant.routingKey, message);
            AjaxResultVO resultVO = AjaxResultHelper.success();
            resultVO.setMsg("发送消息成功");
            return resultVO;
        } catch (Exception e) {
            log.warn("发送消息失败");
            AjaxResultVO resultVO = AjaxResultHelper.error();
            resultVO.setMsg("发送消息异常" + ExceptionUtils.getRootCauseStackTrace(e));
            return resultVO;
        }
    }

    @RequestMapping(value = "/expOrder")
    @ResponseBody
    public AjaxResultVO sendExpOrder(@RequestBody MqMsg mqMsg) {
        log.warn("expOrder msg:{}", mqMsg.toString());

        try {
            byte[] msgBody = mqMsg.toString().getBytes("gbk");
            Message message = new Message(msgBody);
            msgProducter.sendMsg(Constant.routingKey, message);
            AjaxResultVO resultVO = AjaxResultHelper.success();
            resultVO.setMsg("发送消息成功");
            return resultVO;
        } catch (Exception e) {
            log.warn("发送消息失败");
            AjaxResultVO resultVO = AjaxResultHelper.error();
            resultVO.setMsg("发送消息异常" + ExceptionUtils.getRootCauseStackTrace(e));
            return resultVO;
        }
    }

    @RequestMapping(value = "/sendfilemq")
    @ResponseBody
    public AjaxResultVO sendfilemq(@RequestBody MqMsg mqMsg) {
        log.warn("expOrder msg:{}", mqMsg.toString());

        try {
            // byte[] msgBody = mqMsg.toString().getBytes("gbk");
            Message message = new Message(mqMsg.getMsg().toString().getBytes());
            msgProducter.sendMsg("topic_task", "parse.step", message);
            AjaxResultVO resultVO = AjaxResultHelper.success();
            resultVO.setMsg("发送消息成功");
            return resultVO;
        } catch (Exception e) {
            log.warn("发送消息失败");
            AjaxResultVO resultVO = AjaxResultHelper.error();
            resultVO.setMsg("发送消息异常" + ExceptionUtils.getRootCauseStackTrace(e));
            return resultVO;
        }
    }

    @PostMapping(value = "/sendModel/{topicExchange}/{routingKey}")
    public ResultBO sendModel(@PathVariable("topicExchange") String topicExchange, @PathVariable("routingKey") String routingKey
            , @RequestBody Object mqMsg) {
        log.warn("send topicExchange:{},routingKey:{} msg:{}", topicExchange, routingKey, mqMsg.toString());
        try {
            byte[] msgBody = mqMsg.toString().getBytes();
            Message message = new Message(msgBody);
            msgProducter.sendMsg(topicExchange, routingKey, message);
            ResultBO resultVO = ResultHelper.success();
            resultVO.setMsg("发送消息成功");
            return resultVO;
        } catch (Exception e) {
            log.error("发送消息失败", e);
            ResultBO resultVO = ResultHelper.error("发送消息失败");
            return resultVO;
        }
    }

}
